package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.db.DatabaseSchema.DeptGroup;
import com.moleq.dialog.MessageBox;
import com.moleq.models.Group;

public class GroupSettingService {
	private DatabaseHelper DBHelper;
	private Context mContext;
	private String errorString;
	public GroupSettingService() {

	}

	public GroupSettingService(Context context) {
		this.DBHelper = new DatabaseHelper(context);
		mContext = context;
	}
	
	public List<Group> FindAll(){
		List<Group> obj = new ArrayList<Group>();
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = DBHelper.rawQuery("select * from t_dept_group order by dg_no ",null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while(cursor.moveToNext()){
			int dg_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.DeptGroup.DG_NO));
			String dg_name  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.DeptGroup.DG_NAME));
			String createTime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.DeptGroup.CREATE_TIME));
			String updateTime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.DeptGroup.UPDATE_TIME));
			
			obj.add(new Group(dg_no,dg_name,createTime,updateTime));
		}
		cursor.close();
		return obj;
	}
	
	public Group Find(int id){
		Group obj = null;
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = DBHelper.rawQuery("select * from t_dept_group where dg_no = "+id,null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		if(cursor.moveToNext()){
			int dg_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.DeptGroup.DG_NO));
			String dg_name  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.DeptGroup.DG_NAME));
			String createTime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.DeptGroup.CREATE_TIME));
			String updateTime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.DeptGroup.UPDATE_TIME));
			
			obj = new Group(dg_no,dg_name,createTime,updateTime);
		}
		cursor.close();
		return obj;
	}
	
	public boolean InsertGroup(String name) {
		boolean check = true;
		String sql;
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		sql = "insert into t_dept_group values";
		sql += "(" + null + ",'" + name + "',strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now'))";

		errorString = DBHelper.execSQL(sql);

		if (errorString.length()> 0) {
			check = false;
		} 
		return check;
	}

	public boolean UpdateGroup(String name, String dg_no) {
		boolean check = true;
		String sql;
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		sql = "Update t_dept_group ";
		sql += "set dg_name = '" + name + "', update_datetime = strftime('%Y%m%d%H%M%S','now')";
		sql += " where dg_no = " + dg_no;

		errorString = DBHelper.execSQL(sql);

		if (errorString.length()> 0) {
			check = false;
		} 
		return check;

	}
	
	public boolean Checkgroup(String gid) {
		boolean lb_check = true;
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "select dg_no from t_dept_group where dg_name   = '"
				+ gid + "'";
		Cursor cursor = DBHelper.rawQuery(sql,null);
		if(cursor == null){
//			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return true;
		}
		if (cursor.moveToNext()) {

			lb_check = false;
		}
		cursor.close();
		return lb_check;
	}

	public boolean Checkgroupbyid(String id) {
		boolean lb_check = true;
		String sql = "select F263 from t_department_setting where F263  = "+ id;
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = DBHelper.rawQuery(sql,null);
		if(cursor == null){
//			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return true;
		}
		if (cursor.moveToNext()) {

			lb_check = false;
		}
		cursor.close();
		return lb_check;
	}
	
	public boolean DeleteGroup(String group){
		boolean check = true;
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "Delete from t_dept_group where dg_no = " + group;
		errorString = DBHelper.execSQL(sql);

		if (errorString.length()> 0) {
			check = false;
		} 
		return check;
	}

	public long getRowCount() {
		long count = 0;
		try {
			SQLiteDatabase db = DBHelper.getWritableDatabase();
			String strSQL = "select count(*) from "+ DeptGroup.SCHEMA_NAME ;
			Cursor cursor = db.rawQuery(strSQL, null);

			if (cursor.moveToFirst()) {
				count = cursor.getInt(0);
			}
			cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}
	
	public int getMaxNo() {
		int maxGroup = 0;
		try {
			SQLiteDatabase db = DBHelper.getWritableDatabase();
			String strSQL = "select Max(dg_no) from "+ DeptGroup.SCHEMA_NAME ;
			Cursor cursor = db.rawQuery(strSQL, null);

			if (cursor.moveToFirst()) {
				maxGroup = cursor.getInt(0);
			}
			cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return maxGroup;
	}
}
